<?php

require_once $_SERVER['DOCUMENT_ROOT'].'/ad/sys/com/model/util/excel/Classes/PHPExcel.php';
require_once $_SERVER['DOCUMENT_ROOT']."/ad/sys/com/model/util/PHPMailer/class.phpmailer.php";

class Util {
	public static function forward($path) {
		//header("Location:$path");	 
		echo "<script Language=\"JavaScript\">location.href = ('$path');</script>";
	}
	
	public static function alert($str,$code="utf-8")
	{
		echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=$code\">";
		echo "<script language=\"JavaScript\">";
		echo "alert('$str');";
		echo "</script>";
	}
	/**
	 * Object Array to JSON String
	 * Enter description here ...
	 * @param unknown_type $arr
	 */
	public static function toJson($arr) {
		return   json_encode($arr);
	}
	
	/**
	 * JSON String to Array
	 * Enter description here ...
	 * @param unknown_type $json
	 */
	public static function jsonToArray($json){
		return  json_decode($json, true);
	}
	
	/**
	 * JSON String to Object
	 * @param unknown_type $json
	 */
	public static function jsonToObject($json){
		return  json_decode($json);
	}
	
	/**
	 * Get now date
	 * Enter description here ...
	 */
	public static function getNowDate($style="Y/m/d H:i:s") {
		date_default_timezone_set('Asia/Taipei');
		return date($style);
	}
	
	 
	
	
	/**
	 * Export excel
	 * @param unknown_type $filename
	 * @param unknown_type $titles
	 * @param unknown_type $contents
	 */
	public static function excel($filename, $titles, $contents) {
		error_reporting(E_ALL);

		date_default_timezone_set('Europe/London');
		
		$objPHPExcel = new PHPExcel();
		
		$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
									 ->setLastModifiedBy("Maarten Balliauw")
									 ->setTitle("Office 2007 XLSX Test Document")
									 ->setSubject("Office 2007 XLSX Test Document")
									 ->setKeywords("office 2007 openxml php");
		$objPHPExcel->getDefaultStyle()->getFont()->setSize(11);
		$sheet = $objPHPExcel->setActiveSheetIndex(0);
		 
		foreach($titles as $key=>$value) {
			$sheet->setCellValue($key, $value);
		 	$sheet->getColumnDimension($key[0])->setWidth(18);
		};
		 
	           
		foreach ($contents as $obj) {
			foreach ($obj as $key=>$value) {
				$sheet->setCellValue($key, $value);
			}
		}
	 
		//$objPHPExcel->getActiveSheet()->setTitle('Simple');
	 
	 
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename='.$filename.'.xls');
		header('Cache-Control: max-age=0');
		
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output');
	}
	
	/**
	 * Sending Email
	 * @param unknown_type $send
	 * @param unknown_type $subject
	 * @param unknown_type $content
	 */
	public static function email($sends, $subject, $content) {
		$mail = new PHPMailer();
		$mail->IsSMTP();                  // set mailer to use SMTP
		$mail->Host = "mail.mmedia.ai-ad.net.tw";  // specify main and backup server
		$mail->SMTPAuth = true;     // turn on SMTP authentication
		//$mail->SMTPSecure = "ssl";  //use ssl
		$mail->Username = "service@mmedia.ai-ad.net.tw";  // SMTP username
		$mail->Password = "service"; // SMTP password
		$mail->Port = 25;
		$mail->CharSet="utf-8";
		$mail->From = "service@mmedia.ai-ad.net.tw";
		$mail->FromName = "Amazing Mobile ADs";
		
		if (is_array($sends)) {
			foreach($sends as $send) {
				$mail->AddAddress($send);
			}
		} else {
			$mail->AddAddress($sends);
		}
		//$mail->AddAddress("ellen@example.com");                  // name is optional
		//$mail->AddReplyTo("info@example.com", "Information");
		//$mail->WordWrap = 50;                                 
		//$mail->AddAttachment("/var/tmp/file.tar.gz");         // add attachments
		//$mail->AddAttachment("/tmp/image.jpg", "new.jpg");    // optional name
		$mail->IsHTML(true);                                  // set email format to HTML
		$mail->Subject = $subject;
		$mail->Body = $content;
    	return $mail->Send();
	}
} 

 
?>